package com.web;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Main extends Activity {
	private WebView webview = null;
	float aaa;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		webview = (WebView) findViewById(R.id.webview);
		//webview.getSettings().setAllowFileAccess(true);// 设置允许访问文件数据
		webview.getSettings().setJavaScriptEnabled(true);// 设置支持javascript
		webview.setWebViewClient(new WebViewClientDemo());
		// if (NetWork.isNetworkAvailable(Main.this))
		aaa = ((float) getOldBrightness() / (float) 255);
		Log.v("logo", "getOldBrightness() == " + getOldBrightness());
		Log.v("logo", "aaa == " + aaa);
	}

	private class WebViewClientDemo extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.v("logo", "webview.getUrl() == " + webview.getUrl());
		if (!NetWork.isNetworkAvailable(Main.this)) {
			new AlertDialog.Builder(Main.this).setTitle("网络连接有误")
					.setMessage("请点击确定键设置网络")
					.setPositiveButton("确定", new OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							try {
								startActivity(new Intent(
										android.provider.Settings.ACTION_WIRELESS_SETTINGS));
							} catch (NullPointerException e) {
							}
						}
					}).setNegativeButton("取消", new OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							System.exit(0);
						}
					}).show();
		} else {
			if (webview.getUrl() == null || webview.getUrl().equals("null"))
				webview.loadUrl("http://3g.huanale.com");
		} 
	}

	// 创建菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "夜间模式");
		menu.add(0, 1, 1, "刷新本页");
		menu.add(0, 2, 2, "关于");
		menu.add(0, 3, 3, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	// 菜单响应
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 0:
			if (item.getTitle().equals("夜间模式")) {
				brightnessMax(0.10f);
				item.setTitle("日间模式");
			} else {
				brightnessMax(aaa);
				Log.v("logo", "aaa == " + aaa);
				item.setTitle("夜间模式");
			}
			break;
		case 1:
			webview.loadUrl(webview.getUrl());
			break;
		case 2:
			Toast.makeText(Main.this, "您也可以在手机浏览器上通过http://3g.hnl.so使用记账本。",
					Toast.LENGTH_LONG).show();
			break;
		case 3:
			this.finish();
			break;
		}
		return true;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
		}
	}

	/**
	 * 设置屏幕亮度
	 */
	private void brightnessMax(float max) {
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.screenBrightness = max;
		getWindow().setAttributes(lp);
	}

	/**
	 * 按返回键监听
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			showDialog(Main.this, "提示", "确认退出花哪了", "取消", "确认",
					myDialogOnClickLsner);
			break;
		default:
			break;
		}
		return false;
	}

	/**
	 * 退出程序的dialog
	 */
	public static void showDialog(Context context, String tittle,
			String message, String NegativeBtnStr, String PositiveBtnStr,
			DialogInterface.OnClickListener myDialogOnClickLsner) {
		AlertDialog.Builder loginRegDlg = new AlertDialog.Builder(context);
		loginRegDlg.setTitle(tittle).setMessage(message)
				.setNegativeButton(NegativeBtnStr, myDialogOnClickLsner)
				.setPositiveButton(PositiveBtnStr, myDialogOnClickLsner);
		loginRegDlg.show();
	}

	/**
	 * 退出程序时的对话框监听
	 */
	public static DialogInterface.OnClickListener myDialogOnClickLsner = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case DialogInterface.BUTTON_NEGATIVE:
				break;

			case DialogInterface.BUTTON_POSITIVE:
				System.exit(0);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 取得当前用户自定义的屏幕亮度
	 */
	private int getOldBrightness() {
		int brightness;
		try {
			brightness = Settings.System.getInt(getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException snfe) {
			brightness = 255;
		}
		return brightness;
	}
}